# Copyright 2010-2014 David "Alemarius Nexus" Lerch
# 
# This file is part of gtatools-gui.
#
# gtatools-gui is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# gtatools-gui is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with gtatools-gui.  If not, see <http://www.gnu.org/licenses/>.

# Additional permissions are granted, which are listed in the file
# GPLADDITIONS.

CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
PROJECT(prj-gtatools-gui C CXX)

INCLUDE(CheckSymbolExists)

ADD_SUBDIRECTORY(src)

IF(NOT TARGET nxcommon)
    FIND_PACKAGE(Libnxcommon REQUIRED CONFIG)
ENDIF()

IF(NOT TARGET gtaformats)
    FIND_PACKAGE(Libgtaformats REQUIRED CONFIG)
ENDIF()

IF(NOT TARGET gta)
    FIND_PACKAGE(Libgta REQUIRED CONFIG)
ENDIF()

IF(GTAFORMATS_ENABLE_SQUISH)
    FIND_PACKAGE(Libsquish REQUIRED)
ENDIF()

FIND_PATH(LUA_INCLUDE_DIRS lua.h)
FIND_LIBRARY(LUA_LIBRARIES NAMES lua liblua)

IF(GTAFORMATS_ENABLE_PVRTEXLIB)
    FIND_PACKAGE(PVRTexLib REQUIRED)
ENDIF()

FIND_PACKAGE(Bullet REQUIRED COMPONENTS BulletCollision BulletDynamics LinearMath)

IF(GTA_USE_OPENGL_ES)
    FIND_LIBRARY(GLESV2_LIBRARIES NAMES GLESv2 libGLESv2)
    FIND_LIBRARY(EGL_LIBRARIES NAMES EGL libEGL)
    FIND_PATH(GLESV2_INCLUDE_DIRS GLES2/gl2.h)
    FIND_PATH(EGL_INCLUDE_DIRS EGL/egl.h)
ELSE(GTA_USE_OPENGL_ES)
    FIND_PACKAGE(OpenGL)
    FIND_LIBRARY(GLEW_LIBRARIES NAMES GLEW libGLEW glew glew32 libglew libglew32)
    FIND_PATH(GLEW_INCLUDE_DIRS GL/glew.h)
ENDIF(GTA_USE_OPENGL_ES)

FIND_PACKAGE(Qt4 4.6.0 COMPONENTS QtCore QtGui QtXml QtOpenGL QtNetwork qtmain REQUIRED)

IF(WIN32)
    STRING(REGEX MATCH "-lGL" QTOPENGL_DEPS_UNIX "${QT_QTOPENGL_LIB_DEPENDENCIES}")
    
    IF(QTOPENGL_DEPS_UNIX)
        SET(QT_QTOPENGL_LIB_DEPENDENCIES "")
    ENDIF(QTOPENGL_DEPS_UNIX)
ENDIF(WIN32)

INCLUDE(${QT_USE_FILE})

IF(APPLE)
    FIND_PATH(ICONV_INCLUDE_DIRS iconv.h)
    FIND_LIBRARY(ICONV_LIBRARIES NAMES iconv libiconv)
ENDIF(APPLE)

SET(LIBRARIES gta gtaformats nxcommon)
SET(INCLUDES "")

IF(GTAFORMATS_ENABLE_SQUISH)
    SET(LIBRARIES ${LIBRARIES} "${LIBSQUISH_LIBRARIES}")
    SET(INCLUDES ${INCLUDES} "${LIBSQUISH_INCLUDE_DIRS}")
ENDIF(GTAFORMATS_ENABLE_SQUISH)

SET(LIBRARIES ${LIBRARIES} "${LUA_LIBRARIES}")
SET(INCLUDES ${INCLUDES} "${LUA_INCLUDE_DIRS}")

IF(GTAFORMATS_ENABLE_PVRTEXLIB)
    SET(LIBRARIES ${LIBRARIES} "${PVRTEXLIB_LIBRARIES}")
    SET(INCLUDES ${INCLUDES} "${PVRTEXLIB_INCLUDE_DIRS}")
ENDIF(GTAFORMATS_ENABLE_PVRTEXLIB)

IF(GTA_USE_OPENGL_ES)
    SET(LIBRARIES ${LIBRARIES} "${GLESV2_LIBRARIES}" "${EGL_LIBRARIES}")
    SET(INCLUDES ${INCLUDES} "${GLESV2_INCLUDE_DIRS}" "${EGL_INCLUDE_DIRS}")
ELSE(GTA_USE_OPENGL_ES)
    SET(LIBRARIES ${LIBRARIES} "${OPENGL_gl_LIBRARY}" "${GLEW_LIBRARIES}")
    SET(INCLUDES ${INCLUDES} "${OPENGL_INCLUDE_DIR}" "${GLEW_INCLUDE_DIRS}")
ENDIF(GTA_USE_OPENGL_ES)

SET(LIBRARIES ${LIBRARIES} ${BULLET_LIBRARIES})
SET(INCLUDES ${INCLUDES} ${BULLET_INCLUDE_DIRS})

SET(LIBRARIES ${LIBRARIES} "${OPENGL_gl_LIBRARY}")
SET(INCLUDES ${INCLUDES} "${OPENGL_INCLUDE_DIR}")

IF(APPLE)
    SET(LIBRARIES ${LIBRARIES} "${ICONV_LIBRARIES}")
    SET(INCLUDES ${INCLUDES} "${ICONV_INCLUDE_DIRS}")
ENDIF(APPLE)

SET(INCLUDES ${INCLUDES} "${CMAKE_CURRENT_BINARY_DIR}" "${CMAKE_CURRENT_BINARY_DIR}/src")
SET(INCLUDES ${INCLUDES} ${INCS})

IF(UNIX)
    SET(LIBRARIES ${LIBRARIES} "pthread")
    SET(LIBRARIES ${LIBRARIES} "dl")
ENDIF(UNIX)

SET(LIBRARIES ${LIBRARIES} ${QT_LIBRARIES})

SET_SOURCE_FILES_PROPERTIES(gtatools-gui_en.ts gtatools-gui_de.ts PROPERTIES
    OUTPUT_LOCATION "${CMAKE_SOURCE_DIR}/gtatools-gui/src")

QT4_WRAP_CPP(MOC_COMPILED_SRCS ${MOC_SRCS})
QT4_WRAP_UI(UIC_COMPILED_SRCS ${UIC_SRCS})
QT4_ADD_TRANSLATION(QM_FILES gtatools-gui_en.ts gtatools-gui_de.ts)

QT4_ADD_RESOURCES(QT_RESOURCE_FILES "${CMAKE_SOURCE_DIR}/gtatools-gui/src/resources.qrc")

SET(EXECUTABLE_PROPERTIES "")
IF(WIN32)
    SET(EXECUTABLE_PROPERTIES "WIN32")
ELSEIF(APPLE)
    SET(EXECUTABLE_PROPERTIES "MACOSX_BUNDLE")
ENDIF(WIN32)

INCLUDE_DIRECTORIES(${INCLUDES})

ADD_EXECUTABLE(gtatools-gui ${EXECUTABLE_PROPERTIES} ${SRCS} ${MOC_COMPILED_SRCS} ${UIC_COMPILED_SRCS} ${QM_FILES}
        ${QT_RESOURCE_FILES})
TARGET_LINK_LIBRARIES(gtatools-gui ${LIBRARIES})

INSTALL(TARGETS gtatools-gui DESTINATION bin)

